import requests
from util.fonts import get_font_maps
from PIL import Image, ImageFont, ImageDraw
from ddddocr import DdddOcr
import io
from fontTools.ttLib import TTFont

font_maps = {}
ocr = DdddOcr(show_ad=False)

res = requests.get('https://lf6-awef.bytetos.com/obj/awesome-font/c/96fc7b50b772f52.woff2')
with open("./static/fonts/96fc7b50b772f52.woff2", "wb") as f:
    f.write(res.content)

font = TTFont('./static/fonts/96fc7b50b772f52.woff2')
img_font = ImageFont.truetype('./static/fonts/96fc7b50b772f52.woff2', size=20)
for code, name in font.getBestCmap().items():
    img = Image.new("RGB", (25, 25), color=(255, 255, 255))
    draw = ImageDraw.Draw(img)
    draw.text((0, -5), chr(code), fill=(0, 0, 0), font=img_font)
    # img.save(f"./static/fonts/imgs/{name}.png")
    img_bytes = io.BytesIO()
    img.save(img_bytes, format="JPEG")
    result = ocr.classification(img_bytes.getvalue())
    font_maps[chr(code)] = result

print(font_maps)

res = requests.post('https://www.dongchedi.com/motor/pc/sh/sh_sku_list?aid=1839&app_name=auto_web_pc', data={
    'sh_city_name': '全国',
    'page': 1,
    'limit': 20
})
for item in res.json()['data']['search_sh_sku_info_list']:
    result=""
    for i in item['official_price']+item['sh_price']+item['sub_title']:
        if i in font_maps:
            result += font_maps[i]
        else:
            result += i
    print(result)
    break
